package com.lynn.salary.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lynn.salary.entity.Article;
import com.lynn.salary.entity.ArticleType;
import com.lynn.salary.service.IArticleService;
import com.lynn.salary.service.IArticleTypeService;
import com.lynn.salary.util.ObjectFactory;

public class IndexAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IArticleService articleService = (IArticleService) ObjectFactory.getBean("articleService");
	private IArticleTypeService articleTypeService = (IArticleTypeService) ObjectFactory.getBean("articleTypeService");
	private List<Article> articleList;
	private List<ArticleType> articleTypeList;
	
	private String articleTypeId;
	
	private List<Map<String, Object>> map;
	
	private List<Map<String, Object>> allArticleListMap;
	private int perpageLength = 30;
	private long articlelength;
	private String articleTypeName;
	
	

	@Override
	public String execute() throws Exception {
		return super.execute();
	}
	
	public String getAllArticle() {
		try {
			articleTypeList = articleTypeService.findAll();
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
	}
	
	public String getArticlesBytType(){
		
		try {
			map = articleService.findByArticleType(articleTypeId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("articleTypeId:" + articleTypeId);
		System.out.println("getArticlesBytType");
		return "getArticleSuccess";
		
	}
	
	public String showAllArticleByType(){
		try {
			allArticleListMap = articleService.findPartArticleByType(articleTypeId, perpageLength, currentPage);
			ArticleType type = articleTypeService.getByPk(articleTypeId, ArticleType.class);
			if(type == null){
				return "404";
			}
			articleTypeName = type.getName();
			
			long length = articleService.findAllArticleNumber(articleTypeId);
			if(length%perpageLength != 0)
			{
				articlelength = length/perpageLength + 1;
			}
			else
			{
				articlelength = length/perpageLength;
			}
			return "findAllArticleSuccess";
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
	}
	
	public String getPerpageArticleByType(){
		try {
			allArticleListMap = articleService.findPartArticleByType(articleTypeId, perpageLength, currentPage);
			//articlelength = articleService.findAllArticleNumber(articleTypeId);
			return "getPerpageArticleSuccess";
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
	}

	public List<Article> getArticleList() {
		return articleList;
	}

	public void setArticleList(List<Article> articleList) {
		this.articleList = articleList;
	}

	public List<ArticleType> getArticleTypeList() {
		return articleTypeList;
	}

	public void setArticleTypeList(List<ArticleType> articleTypeList) {
		this.articleTypeList = articleTypeList;
	}
	public List<Map<String, Object>> getMap() {
		return map;
	}

	public void setMap(List<Map<String, Object>> map) {
		this.map = map;
	}

	public String getArticleTypeId() {
		return articleTypeId;
	}

	public void setArticleTypeId(String articleTypeId) {
		this.articleTypeId = articleTypeId;
	}

	public List<Map<String, Object>> getAllArticleListMap() {
		return allArticleListMap;
	}

	public void setAllArticleListMap(List<Map<String, Object>> allArticleListMap) {
		this.allArticleListMap = allArticleListMap;
	}

	public int getPerpageLength() {
		return perpageLength;
	}

	public void setPerpageLength(int perpageLength) {
		this.perpageLength = perpageLength;
	}

	public long getArticlelength() {
		return articlelength;
	}

	public void setArticlelength(long articlelength) {
		this.articlelength = articlelength;
	}

	public String getArticleTypeName() {
		return articleTypeName;
	}

	public void setArticleTypeName(String articleTypeName) {
		this.articleTypeName = articleTypeName;
	}
		
}
